package com.rfid.afd.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.rfid.afd.activity.adapter.ShunjiaAdapter;
import com.rfid.afd.activity.view.IShunjiaView;
import com.rfid.afd.presenter.ShunjiaPresenter;
import com.rfid.db.bean.Rfid;
import com.rfid.rls_psw.R;
import com.rfid.util.LogUtils;
import com.rfid.util.RfidManager;
import com.rfid.widget.LoadingView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
//顺架，先扫描层标，接着扫描第一本书，再扫描第二本书，得到第一本与第二本书之间的所有书，把层架标信息写入这些书
public class ShunjiaActivity extends BaseActivity<ShunjiaPresenter> implements IShunjiaView {

    @BindView(R.id.shelf)
    TextView mTextView;
    @BindView(R.id.start)
    TextView mStart;
    @BindView(R.id.end)
    TextView mEnd;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.scan)
    Button mScanButton;
    @BindView(R.id.quit)
    Button mQuit;
    Rfid mStartRfid;
    Rfid mEndRfid;
    private List<Rfid> mRfids;
    private ShunjiaAdapter mAdapter;
    private String mShelf;
    private long mLastTime;

    @BindView(R.id.loading)
    LoadingView mLoadingView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shunjia);
        ButterKnife.bind(this);
        mRfids = new ArrayList<>();
        mAdapter = new ShunjiaAdapter(this,mRfids);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,RecyclerView.VERTICAL));
        mRecyclerView.setAdapter(mAdapter);
        LayoutAnimationController controller = new LayoutAnimationController(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        mRecyclerView.setLayoutAnimation(controller);
    }

    @Override
    public ShunjiaPresenter getPresenter() {
        return new ShunjiaPresenter();
    }

    @Override
    public void onScaned(String barcode, Rfid rfid) {
        if(rfid == null) {
            return;
        }
        if(System.currentTimeMillis() - mLastTime < 500) {
            return;
        }
        mLastTime = System.currentTimeMillis();
        if(mStartRfid == null) {
            startScan(false);
            mStartRfid = rfid;
            mStart.setText("开始索引号:" + mStartRfid.getRindex());
            mEnd.setText("请扫描下一书架第一本书");
        } else if(mEndRfid == null) {
            if(mStartRfid.getRindex().equals(rfid.getRindex())) {
                return;
            }
            startScan(false);
//            mScanButton.setText("重置");
            mScanButton.setEnabled(false);
            mEndRfid = rfid;
            mEnd.setText("结束索引号:" + mEndRfid.getRindex());
            mPresenter.getRfidsByIndexRanger(mStartRfid,mEndRfid);
        }
    }

    @Override
    public void onShelf(String shelf) {
        mLastTime = System.currentTimeMillis();
        startScan(false);
        mShelf = shelf;
        mTextView.setText("层架:" + mShelf);
        mStart.setText("请扫描层架第一本书");
    }

    @Override
    public void onRfids(List<Rfid> rfids) {
        mRfids.addAll(rfids);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onShunjiaComplete() {
        Toast.makeText(this,"顺架完成",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {
        mLoadingView.setVisibility(View.VISIBLE);
    }

    @Override
    public void stopLoading() {
        mLoadingView.setVisibility(View.GONE);
    }

    boolean isScan;
    public void scan(View view) {
        startScan(!isScan);
    }

    private void startScan(boolean start) {
        TextView textView = (TextView) mScanButton;
        if(start) {
            textView.setText("停止");
            mPresenter.startScanRfid();
            mQuit.setEnabled(false);
        } else {
            textView.setText("扫描");
            mPresenter.stopScanRfid();
            mQuit.setEnabled(true);
        }
        isScan = start;
    }

    public void shunjia(View view) {
        mPresenter.shunjia(mShelf,mRfids,mStartRfid,mEndRfid);
    }

    @Override
    public void finish() {
        super.finish();
        mPresenter.stopScanRfid();
    }

    @Override
    public void onBackPressed() {
        if(isScan) {
            return;
        }
        super.onBackPressed();
        mPresenter.stopScanRfid();
    }

    public void quit(View view) {
        onBackPressed();
    }
}